package com.klei.controller;

import com.klei.service.IMyOrdersService;
import com.klei.util.LayuiJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
public class MyOrdersController {

    @Autowired
    private IMyOrdersService myOrdersService;

    @GetMapping("/getMyOrders/{uid}")
    @ResponseBody
    public LayuiJson getMyOrders(HttpServletRequest servletRequest,
                                 @PathVariable("uid") int uid){

        LayuiJson layuiJson = new LayuiJson();
        layuiJson.setCode(0);
        layuiJson.setMsg("");
        layuiJson.setData(myOrdersService.getMyOrders(uid));

        return layuiJson;
    }

    @GetMapping("/getMyOrdersByPage")
    @ResponseBody
    public LayuiJson getMyOrdersByPage(HttpServletRequest servletRequest,
                                       @RequestParam(name = "uid") int uid,
                                       @RequestParam(name = "page") int page,
                                       @RequestParam(name = "limit") int limit){

        LayuiJson layuiJson = new LayuiJson();
        layuiJson.setCode(0);
        layuiJson.setMsg("");
        layuiJson.setData(myOrdersService.getMyOrdersByPage(uid, (page - 1) * limit, limit));
        layuiJson.setCount(myOrdersService.getMyOrdersCount(uid));

        return layuiJson;
    }

    @DeleteMapping("/deleteMyOrder/{id}")
    @ResponseBody
    public boolean deleteMyOrder(@PathVariable("id") int id){

        return myOrdersService.deleteMyOrder(id);
    }
}
